﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
using JYH.Framework;

namespace JYH.Framework
{
    /// <summary>
    /// 缓存服务
    /// </summary>
    public class CacheHelper<T> where T : class
    {
        private static readonly Cache _cache;
        private static readonly object __obj = new object();

        /// <summary>
        /// 静态缓存类初始化
        /// </summary>
        static CacheHelper()
        {
            HttpContext content = HttpContext.Current;
            if (content != null)
            {
                _cache = content.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }

        /// <summary>
        /// 获取所有缓存Key
        /// </summary>
        /// <returns></returns>
        public static List<string> GetCache()
        {
            List<string> list = new List<string>();
            IDictionaryEnumerator cacheEnum = _cache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                list.Add(cacheEnum.Key.ToString());
            }
            return list;
        }

        /// <summary>
        /// 返回缓存中指定Key的对象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get(string key)
        {
            return (T)_cache[key];
        }

        /// <summary>
        /// 获取缓存对象(根据配置判断是否缓存)
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>object对象</returns>
        public static T GetCache(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;
            if (ConfigHelper.GetValue("IsCache") == "false")
            {
                return null;
            }
            return (T)_cache.Get(key);
        }

        /// <summary>
        /// 判断缓存对象是否存在
        /// </summary>
        /// <param name="cacheKey">缓存键值名称</param>
        /// <returns>是否存在true 、false</returns>
        public static bool IsExist(string cacheKey)
        {
            return HttpContext.Current.Cache[cacheKey] != null;
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }

        /// <summary>
        /// 删除指定Key缓存
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            _cache.Remove(key);
        }

        /// <summary>
        /// 对象插入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="cacheDays"></param>
        public static void Insert(string key, T obj, int cacheDays = 1)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, DateTime.UtcNow.AddDays(cacheDays), TimeSpan.Zero);
            }
        }

        /// <summary>
        /// 创建缓存项的文件依赖
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="fileName"></param>
        public static void InsertFile(string key, T obj, string fileName)
        {
            //创建缓存依赖
            CacheDependency dep = new CacheDependency(fileName);
            //创建缓存
            _cache.Insert(key, obj, dep);
        }

        /// <summary>
        /// 创建缓存项过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">object对象</param>
        public static void Insert(string key, T obj)
        {
            if (obj != null)
            {
                int expires = ConfigHelper.GetValue("TimeCache").ToInt();
                Insert(key, obj, expires);
            }
        }
        /// <summary>
        /// 创建缓存项过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="obj">object对象</param>
        /// <param name="expires">设置时间</param>
        public static void Insert(string key, object obj, int expires)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
            }
        }
    }
}
